package org.store.controller;



import com.alibaba.fastjson.JSONObject;
import jakarta.servlet.http.HttpServletRequest;

import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.store.HttpGetJson;
import org.store.domain.User;
import org.store.service.impl.UserServiceImpl;

import java.util.UUID;

@Controller
public class login {
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private UserServiceImpl userService;
    @Autowired
    private JavaMailSenderImpl mailSender;

    private int verification_code;
    @GetMapping("/home")
    public String home(Model model){
        return "home.html";
    }

    @PostMapping("/session")
    @ResponseBody
    public String login(){
        JSONObject jsonObject= HttpGetJson.getJson(request);
        String account= jsonObject.getString("account");
        String password= jsonObject.getString("password");
        User user=userService.login(new User(account,password));
        System.out.println("我叫slc");
        if(user==null){
            return null;
        }else{
            String token= UUID.randomUUID().toString();
            HttpSession session= request.getSession();
            session.setAttribute("token",token);
            return token;
        }
    }

    @PostMapping("/user")
    @ResponseBody
    public String register(){
        JSONObject jsonObject= HttpGetJson.getJson(request);
        String account= jsonObject.getString("account");
        String email= jsonObject.getString("email");
        String password= jsonObject.getString("password");
        userService.register(new User(account,password,email));
        return "true";
    }

    @GetMapping("/user/{account}/account")
    @ResponseBody
    public String checkAccount(@PathVariable("account")String account){
        if(userService.checkUsername(account)){
            return "true";
        }else{
            return "false";
        }
    }

    @GetMapping("/user/{email}/email")
    @ResponseBody
    public String checkEmail(@PathVariable("email")String email){
        if(userService.checkEmail(email)){
            return "true";
        }else{
            return "false";
        }
    }

    @GetMapping("/code/{email}")
    @ResponseBody
    public void sendCode(@PathVariable String email){
        verification_code=(int)((Math.random()*9+1)*10000);
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        simpleMailMessage.setFrom("1084884210@qq.com");
        simpleMailMessage.setTo(email);
        simpleMailMessage.setSubject("文享图书管理系统");
        simpleMailMessage.setText("亲爱的用户您好！您的注册验证码是:"+verification_code+"\n" +
                "若非您本人操作，请忽略这条邮件！");
        mailSender.send(simpleMailMessage);
    }

    @GetMapping("/code/{email}/{code}")
    @ResponseBody
    public String checkCode(@PathVariable String email,@PathVariable String code){
        if(Integer.parseInt(code)==verification_code){
            return "true";
        }else{
            return "false";
        }
    }
}
